package P062_ShallowCopyOfArray;

import org.junit.Test;

import java.util.Arrays;

/**
 * 数组的浅拷贝
 * 基本类型都是拷贝值,其他都是拷贝引用地址
 */
public class ShallowCopyOfArray {

    enum Color{
        Red,
        Orange,
        Yellow,
        Green,
        Indigo,
        Blue,
        Violet
    }

    class Balloon{
        private int id;
        private Color color;

        public Balloon(int id, Color color) {
            this.id = id;
            this.color = color;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public Color getColor() {
            return color;
        }

        public void setColor(Color color) {
            this.color = color;
        }

        @Override
        public String toString() {
            return "Balloon{" +
                    "id=" + id +
                    ", color=" + color +
                    '}';
        }
    }

    @Test
    public void Test() {
        Balloon[] balloons = new Balloon[7];

        //填入七种颜色的气球
        for (int i = 0; i < balloons.length; i++) {
            balloons[i] = new Balloon(i, Color.values()[i]);
        }

        Balloon[] copyBalloons = Arrays.copyOf(balloons, balloons.length);

        //将拷贝出来的第七个气球改为蓝色
        copyBalloons[6].setColor(Color.Blue);

        //打印被拷贝的气球数组
        for (int i = 0; i < balloons.length; i++) {
            System.out.println(balloons[i].toString());
        }
    }
}
